"""
Case Type   : 服务端工具
Case Name   : 验证gs_dumpall工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.显示帮助信息
    2.显示版本号
    3.指定文件导出
Expect      :
    1.显示正确
    2.显示正确
    3.成功
History     :
"""
import unittest
import os
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger



class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_server_tools1(self):
        self.log.info('--------step1:显示帮助信息;except:显示正确------')
        cmd_list = ['-?', '--help']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                gs_dumpall {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.log.info('--------------校验语法------------------')
            self.assertIn('gs_dumpall [OPTION]...', msg1)
            self.log.info('--------------校验参数存在与否------------------')
            start_index = msg1.find('General options:')
            options_list = []
            for i in msg1[start_index:].split('\n'):
                for j in i.split(' '):
                    if len(j) != 0:
                        if j[0] == '-':
                            options_list.append(j)
                        else:
                            pass
                    else:
                        pass
            self.log.info(options_list)
            parameter = [
                '-f,', '--file=FILENAME', '-v,', '--verbose', '-V,', '--version', '--lock-wait-timeout=TIMEOUT', '-?,',
                '--help', '-a,', '--data-only', '-c,', '--clean', '-g,', '--globals-only', '-o,', '--oids', '-O,',
                '--no-owner', '-r,', '--roles-only', '-s,', '--schema-only', '-S,', '--sysadmin=NAME', '-t,',
                '--tablespaces-only', '-x,', '--no-privileges', '--column-inserts/--attribute-inserts',
                '--disable-dollar-quoting', '--disable-triggers', '--inserts', '--no-security-labels',
                '--no-tablespaces', '--no-unlogged-table-data', '--include-alter-table', '--quote-all-identifiers',
                '--dont-overwrite-file', '--use-set-session-authorization', '--with-encryption=AES128', '--with-key=KEY',
                '--include-templatedb', '--binary-upgrade', '--binary-upgrade-usermap="USER1=USER2"', '--non-lock-table',
                '--tablespaces-postfix', '--parallel-jobs', '-h,', '--host=HOSTNAME', '-l,', '--database=DBNAME', '-p,',
                '--port=PORT', '-U,', '--username=NAME', '-w,', '--no-password', '-W,', '--password=PASSWORD',
                '--role=ROLENAME', '--rolepassword=ROLEPASSWORD']
            if len(options_list) == len(parameter):
                for opt in options_list:
                    if opt in parameter:
                        self.log.info(f'{opt}---------参数校验通过---------')
                    else:
                        self.log.error(f'{opt}---------参数校验不通过---------')
            else:
                self.log.error('---------参数校验有误---------')

        self.log.info('------------------显示版本号信息------------------')
        cmd_list = ['-V', '--version']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                                gs_dumpall {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.assertIn('openGauss', msg1)

        self.log.info('------------创建数据-------------')
        sql_cmd1 = '''
        drop table if exists test1; 
        create table test1 (id int ,name char(10));
        insert into test1 values (1,'aa'),(2,'bb');
        '''
        gsql_cmd1 = f'''source {macro.DB_ENV_PATH} ;
            gsql -d {self.primary_node.db_name} -p\
            {self.primary_node.db_port} -c "{sql_cmd1}"
            '''
        self.log.info(gsql_cmd1)
        sql_msg1 = self.primary_node.sh(gsql_cmd1).result()
        self.log.info(sql_msg1)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_msg1)

        self.log.info('--只导出数据到指定文件--')
        dumpall_cmd = f"source {macro.DB_ENV_PATH};" \
            f"gs_dumpall " \
            f"-p {self.primary_node.db_port} " \
            f"-a " \
            f"-f {macro.DB_INSTANCE_PATH}/dumpall.sql;"
        self.log.info(dumpall_cmd)
        dumpall_msg = self.primary_node.sh(dumpall_cmd).result()
        self.log.info(dumpall_msg)
        self.assertIn(self.constant.gs_dumpall_success_msg, dumpall_msg)
        cat_cmd = f'cat {macro.DB_INSTANCE_PATH}/dumpall.sql;'
        self.log.info(cat_cmd)
        cat_msg = self.primary_node.sh(cat_cmd).result()
        self.log.info(cat_msg)
        self.assertIn('COPY public.test1 (id, name) FROM stdin;', cat_msg)
        
    def tearDown(self):
        self.log.info('--------------无需清理环境-------------------')
        self.log.info(f'-----{os.path.basename(__file__)} end------')